<?php
namespace hema\delivery;

use hema\delivery\engine\Uu;
use hema\delivery\engine\Dada;
use hema\delivery\engine\Sf;
use hema\delivery\engine\Make;

/**
 * 第三方配送模块驱动
 */
class Driver
{
    private $engine;    // 当前引擎类

    /**
     * 构造方法
     */
    public function __construct($type = '', $order = [])
    {
        $this->engine = $this->getEngineClass($type,$order);// 实例化当前引擎
    }

    /**
     * 获取当前的引擎
     */
    private function getEngineClass($type, $order)
    {
        if($type == 'uu'){
            return new Uu($order);
        }
        if($type == 'sf'){
            return new Sf($order);
        }
        if($type == 'dada'){
            return new Dada($order);
        }
        if($type == 'make'){
            return new Make($order);
        }
        die(json_encode(['code' => 0, 'msg' => '未找到引擎类:' . $type]));
    }

    /**
     * 预发布订单
     */
    public function preOrder()
    {
        return $this->engine->preOrder();
    }

    /**
     * 添加订单
     */
    public function addOrder($data = [])
    {
        return $this->engine->addOrder($data);
    }
    /**
     * 取消订单
     */
    /**
     * 添加订单
     */
    public function cancelOrder($order_no = '')
    {
        return $this->engine->cancelOrder($order_no);
    }

    /**
     * 获取错误信息
     * @return mixed
     */
    public function getError()
    {
        return $this->engine->getError();
    }

}
